package org.jks.service.impl;

import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.jks.model.Privilege;
import org.springframework.stereotype.Repository;

@Repository("PrivilegeServiceImpl")
public class PrivilegeServiceImpl extends BaseDaoImpl<Privilege> {
	public void save(List<Privilege> privileges) {
		Session session = this.openSession();
		session.beginTransaction();
		for (Privilege privilege : privileges) {
			session.save(privilege);
		}
		session.getTransaction().commit();
		session.flush();
		session.close();
	}

	public void update(List<Privilege> privileges) {
		Session session = this.openSession();
		for (Privilege privilege : privileges) {
			session.update(privilege);
		}
		session.flush();
		session.close();
	}

	public List<Privilege> all() {
		return find("from Privilege");
	}

	public List<Privilege> listpage(int page, int pagesize) {
		List<Object> param = null;
		return find("from Privilege", param, page, pagesize);
	}

	public long count() {
		return count("select count(*) from Privilege");
	}

	public Privilege findById(long id) {
		return get(Privilege.class, id);
	}

	public void delete(long id) {
		Session session = this.openSession();
		session.delete(session.load(Privilege.class, id));
		session.flush();
		session.close();
	}

	public void delete(long[] ids) {
		Session session = this.openSession();
		StringBuilder strIds = new StringBuilder();
		strIds.append('(');
		int imax = ids.length - 1;
		for (int i = 0;; i++) {
			strIds.append(ids[i]);
			if (i == imax) {
				strIds.append(')');
				break;
			}
			strIds.append(", ");
		}
		SQLQuery sqlQuery = session
				.createSQLQuery("delete from privilege where id in " + strIds);
		sqlQuery.executeUpdate();
		session.flush();
		session.close();
	}
}
